package com.example.dryclean_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dryclean_admin.common.PasswordUtils;
import com.example.dryclean_admin.common.QueryPageParam;
import com.example.dryclean_admin.common.Result;
import com.example.dryclean_admin.entity.Menu;
import com.example.dryclean_admin.entity.User;
import com.example.dryclean_admin.service.IMenuService;
import com.example.dryclean_admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private IMenuService iMenuService;

    @RequestMapping ("/userlist")
    public Result list(){
        return Result.success(userService.list());
    }

    @GetMapping("/findByAccount")
    public Result findByAccount(@RequestParam String account){
        List list = userService.lambdaQuery().eq(User::getAccount,account).list();
        return list.size()>0 ? Result.success(list) : Result.fail();
    }
    //登录
    @RequestMapping("/login")
    public Result login(@RequestParam("account") String account, @RequestParam("password") String password){
       //1.非空校验
        if(!StringUtils.hasLength(account) || !StringUtils.hasLength((password))){
            return Result.fail();
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .eq(User::getAccount,account);
        User user = userService.getOne(wrapper);
        if((user != null) && user.getPassword().equals(password)){
                List menuList = iMenuService.lambdaQuery().like(Menu::getMenuRight,user.getRoleid()).list();
                HashMap res = new HashMap();
                res.put("user",user);
                res.put("menu",menuList);
                return Result.success(res);
        }
        return Result.fail();
    }
   /* public Result login(@RequestParam("account") String account, @RequestParam("password") String password){
        //1.非空校验
        if(!StringUtils.hasLength(account) || !StringUtils.hasLength((password))){
            return Result.fail();
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .eq(User::getAccount,account);
        User user = userService.getOne(wrapper);
        if(PasswordUtils.check(password,user.getPassword())){
            if(user != null){
                List menuList = iMenuService.lambdaQuery().like(Menu::getMenuRight,user.getRoleid()).list();
                HashMap res = new HashMap();
                res.put("user",user);
                res.put("menu",menuList);
                return Result.success(res);
            }
        }

        return Result.fail();
    }
*/
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        return userService.save(user)?Result.success():Result.fail();
    }

    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return userService.removeById(id) ? Result.success() : Result.fail();
    }

    //注册
    @PostMapping ("/reg")
    public Result reg(@RequestBody User user){
        List<User> list = userService.lambdaQuery()
                .eq(User::getAccount,user.getAccount()).list();
        if(list.isEmpty()){
            /*user.setPassword(PasswordUtils.encrypt(user.getPassword()));*/
            return userService.save(user)?Result.success(user):Result.fail();
        }
       return Result.fail("用户已存在");
    }
   /* public Result reg(@RequestBody User user){
        List<User> list = (List<User>) userService.lambdaQuery()
                .eq(User::getAccount,user.getAccount());
        if(!list.isEmpty()){
            return Result.fail("用户已存在");
        }
        return userService.save(user)?Result.success(user):Result.fail();
        //return Result.success(userService.reg(user));
    }*/

    //更新
    @PostMapping("/update")
    public Result update(@RequestBody User user){
        return userService.updateById(user)?Result.success():Result.fail();
    }

    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody User user){
        return userService.updateById(user);
    }
    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody User user){
        return userService.saveOrUpdate(user);
    }
    //删除
    @GetMapping("/delete")
    public boolean delete(Integer id){
        return userService.removeById(id);
    }
    //查询
    @PostMapping("/listP")
    public List<User> listP(@RequestBody(required = false) User user){
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(user.getUsername())){
            lambdaQueryWrapper.like(User::getUsername,user.getUsername());
        }
        return userService.list(lambdaQueryWrapper);
    }
    @PostMapping("/listPage")
    public List<User> listPage(@RequestBody QueryPageParam query){
        /*System.out.println(query);*/
        System.out.println("num==="+query.getPageNum());
        System.out.println("size==="+query.getPageSize());
        HashMap param = query.getParam();
        String username = (String)param.get("username");
        System.out.println("name==="+(String) param.get("username"));
        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(User::getUsername,username);
        IPage result = userService.page(page,lambdaQueryWrapper);
        System.out.println("total==" + result.getTotal());


        return result.getRecords();
    }

    @PostMapping("/listPageR")
    public Result listPageR(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String username = (String)param.get("username");
        String sex = (String)param.get("sex");
        /*System.out.println("name==="+(String) param.get("username"));*/
        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasLength(username) && !"null".equals(username)){
            lambdaQueryWrapper.like(User::getUsername,username);
        }
        if(StringUtils.hasLength(sex)){
            lambdaQueryWrapper.eq(User::getSex,sex);
        }
        IPage result = userService.page(page,lambdaQueryWrapper);
        System.out.println("total==" + result.getTotal());


        return Result.success(result.getRecords(), result.getTotal());
    }

}
